package ${pkg}.dbservice.impl;

import lombok.*;
import com.boe.common.util.*;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.baomidou.mybatisplus.extension.service.impl.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import ${pkg}.entity.*;
import ${pkg}.mapper.*;
import ${pkg}.dbservice.*;
import ${pkg}.model.vo.${sc(entityName)}.*;
import com.boe.common.mvc.*;
import org.apache.logging.log4j.util.*;
import org.springframework.stereotype.*; 
import java.util.*;
import java.time.*;
<#assign systemFieldNames = ["createBy", "updateBy", "isDeleted", "updateAt", "createAt"]>
/**
 * ${entityCn}
 * Created by ${user} at ${now}
 */
@Service
@RequiredArgsConstructor
public class ${bc(entityName)}ServiceImpl extends ServiceImpl<${bc(entityName)}Mapper, ${bc(entityName)}> implements I${bc(entityName)}Service {

    @Override
    public ListPageResult<${bc(entityName)}ResponseVO> list(${bc(entityName)}RequestVO queryBody, Integer pageNo, Integer pageSize, Map<String, String> order) {

        var queryWrapper = Wrappers.<${bc(entityName)}ResponseVO>query();
        if (queryBody != null) {

        <#list fields as field>

            <#assign dbField = fieldDbField(field) >
            <#assign queryOp = dbField?contains("name")?string("like","eq")>
            <#if  systemFieldNames?seq_contains("${field.name}")>
            <#elseif isType(field,"LocalDate","LocalDateTime","Date","LocalTime")>
            <#else>
            if (ObjectUtil.hasValue(queryBody.get${bc(field.name)}())) {
                queryWrapper = queryWrapper.${queryOp}("${dbField}", queryBody.get${bc(field.name)}());
            }
            </#if>
        </#list>
        }

        if (order != null) {
            for (var orderKey : order.keySet()) {
                var isDesc = "desc".equalsIgnoreCase(order.get(orderKey));
                if (isDesc) {
                    queryWrapper = queryWrapper.orderByDesc(orderKey);
                } else {
                    queryWrapper = queryWrapper.orderByAsc(orderKey);
                }
            }
        }


        var page = new Page<${bc(entityName)}ResponseVO>(pageNo != null ? pageNo : 1, pageSize != null ? pageSize : 99);

        var ret = new ListPageResult<${bc(entityName)}ResponseVO>();
        ret.setData(this.baseMapper.list(page, queryWrapper));
        ret.setTotal(page.getTotal());
        return ret;
    }

    @Override
    public boolean update(String id, ${bc(entityName)}  ${sc(entityName)} ) {
        var where = Wrappers.<${bc(entityName)}>query();
        where.eq("id", id);

        return this.update(${sc(entityName)}, where);
    }
}
